package com.yu.factory;

import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @PackageName: com.yu.factory
 * @Description:
 * @Version: v1.0.0
 * @Author: yuweilin
 * @Date: 16:00
 * <p>
 * Modification History:
 * Date         Author          Version            Description
 * ------------------------------------------------------------
 * 2021/1/24      yuweilin         v1.0.0               新建
 */
@Slf4j
public class SingletonFactory {
    private static final Map<String,Object> OBJECT_MAP = new ConcurrentHashMap<>();
    private SingletonFactory(){}
    public static <T> T getInstance(Class<T> c){
        String key = c.toString();
        Object instance;
        synchronized (SingletonFactory.class){
            instance = OBJECT_MAP.get(key);
            if (instance==null){
                try {
                    instance = c.getDeclaredConstructor().newInstance();
                    OBJECT_MAP.put(key,instance);
                }catch (Exception e){
                    log.error("cuowu?");
                }
            }
        }
        return c.cast(instance);
    }
}
